package com.jeomo.mem.provider.web;

import com.jeomo.base.provider.controller.BaseController;
import com.jeomo.common.core.dto.PageQueryDto;
import com.jeomo.common.core.dto.PageResponseDto;
import com.jeomo.common.core.request.PageRequest;
import com.jeomo.common.core.result.CommonResult;
import com.jeomo.common.core.util.BeanCopyUtil;
import com.jeomo.common.core.vo.PageResponseVo;
import com.jeomo.common.log.annotation.LogRecord;
import com.jeomo.mem.provider.enums.MemberErrorCodeEnum;
import com.jeomo.mem.provider.request.MemberCardRequest;
import com.jeomo.mem.provider.request.MemberCardStatus;
import com.jeomo.mem.provider.request.MemberUpdateReq;
import com.jeomo.mem.provider.vo.MemberCardVo;
import com.jeomo.mem.provider.vo.MemberVo;
import com.jeomo.mem.starter.dto.MemberInfoDto;
import com.jeomo.mem.starter.entity.Member;
import com.jeomo.mem.starter.entity.MemberCard;
import com.jeomo.mem.starter.enums.MemberCardStatusEnum;
import com.jeomo.mem.starter.service.IMemberCardService;
import com.jeomo.shiro.util.LoginSessionUtil;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 会员库接口
 *
 * @author:Jackpot
 * @create 2020-10-30 16:24
 */
@RestController
@RequestMapping("/member/card")
public class MemberCardController extends BaseController {

    @Autowired
    IMemberCardService memberCardService;

    @PostMapping("open")
    @ApiOperation("会员开卡接口")
    public CommonResult openCard(@RequestBody @Validated MemberCardRequest memberCardRequest) {
        //如果是平台注册
        MemberInfoDto memberInfoDto = BeanCopyUtil.copyProperties(memberCardRequest, MemberInfoDto.class);
        int i = memberCardService.registerMemberCard(memberInfoDto);
        return i>0?CommonResult.success("注册成功"):CommonResult.failure("注册失败");
    }

    @PostMapping("/page")
    @ApiOperation("分页查询")
    @LogRecord(module="会员模块",type = "分页查询", memo = "备注信息")
    public CommonResult<PageResponseVo<MemberCardVo>> page(@RequestBody @ApiParam("分页查询参数") PageRequest pageRequest) {
        PageQueryDto dto = BeanCopyUtil.copyProperties(pageRequest, PageQueryDto.class);
        PageResponseDto responseDto = memberCardService.pageSearch(dto);
        PageResponseVo<MemberCardVo> memCardResponseVo = BeanCopyUtil.copyPageProperties(responseDto, MemberCardVo.class);
        return CommonResult.success(memCardResponseVo);
    }

    @ApiOperation("根据当前组织和手机号/会员卡号,查询会员卡信息")
    @GetMapping("/no/phone/{number}")
    public CommonResult<MemberCardVo> queryMemCardByPhoneOrNo(@PathVariable String number){
        String orgCode = LoginSessionUtil.getCurrentLoginInfo().getOrgCode();
        MemberCard memberCard = memberCardService.queryMemCardByPhoneOrNo(orgCode,number);
        MemberCardVo memberCardVo = BeanCopyUtil.copyProperties(memberCard, MemberCardVo.class);
        return CommonResult.success(memberCardVo);
    }
    @ApiOperation("根据当前组织和手机号,查询会员卡信息")
    @GetMapping("/info/phone/{phone}")
    public CommonResult<MemberCardVo> getMemCardByPhoneDef(@PathVariable String phone){
        String orgCode = LoginSessionUtil.getCurrentLoginInfo().getCurrentOrgCode();
        MemberCard memberCard = memberCardService.queryMemCardByPhone(orgCode, phone);
        MemberCardVo memberCardVo = BeanCopyUtil.copyProperties(memberCard, MemberCardVo.class);
        return CommonResult.success(memberCardVo);
    }
    @ApiOperation("根据组织和手机号,查询会员卡信息")
    @GetMapping("/info/{mainOrg}/{phone}")
    public CommonResult<MemberCardVo> getMemCardByPhone(@PathVariable String mainOrg,@PathVariable String phone){
        MemberCard memberCard = memberCardService.queryMemCardByPhone(mainOrg, phone);
        MemberCardVo memberCardVo = BeanCopyUtil.copyProperties(memberCard, MemberCardVo.class);
        return CommonResult.success(memberCardVo);
    }

    @ApiOperation("根据会员卡号,查询会员卡信息")
    @GetMapping("/info/no/{cardNo}")
    public CommonResult<MemberCardVo> getMemCardByNo(@PathVariable String cardNo){
        MemberCard memberCard = memberCardService.queryMemCardByNo(cardNo);
        MemberCardVo memberCardVo = BeanCopyUtil.copyProperties(memberCard, MemberCardVo.class);
        return CommonResult.success(memberCardVo);
    }

    @ApiOperation("查询会员基本信息")
    @GetMapping("/base/{topOrg}/{phone}")
    public CommonResult<MemberVo> getMemberByPhone(@PathVariable String topOrg, @PathVariable String phone){
        Member member = memberCardService.queryMemberByPhone(topOrg, phone);
        MemberVo memberVo = BeanCopyUtil.copyProperties(member, MemberVo.class);
        return CommonResult.success(memberVo);
    }


    @PostMapping("/base")
    @ApiOperation("修改会员基本信息")
    public CommonResult updateMemberInfo(@RequestBody MemberUpdateReq memberUpdateReq) {

        Member member = BeanCopyUtil.copyProperties(memberUpdateReq, Member.class);
        member.setCode(memberUpdateReq.getMemberNo());
        member.setVersion(memberUpdateReq.getMemberVersion());
        int i = memberCardService.updateMemberInfo(member);
        return i>0?CommonResult.success("操作成功"):CommonResult.failure("操作失败");
    }

    @PostMapping("/freeze")
    @ApiOperation("会员冻结接口")
    public CommonResult freeze(@RequestBody @ApiParam("会员信息修改表单") MemberCardStatus memberCardStatus) {
        String cardNo = memberCardStatus.getCardNo();
        String freezedCode = MemberCardStatusEnum.FREEZED.getCode();
        int i = memberCardService.updateCardStatus(cardNo, freezedCode);
        return i>0?CommonResult.success("操作成功"):CommonResult.failure("操作失败");
    }
    @PostMapping("/logout")
    @ApiOperation("会员注销接口")
    public CommonResult logout(@RequestBody @ApiParam("会员信息修改表单") MemberCardStatus memberCardStatus) {
        String cardNo = memberCardStatus.getCardNo();
        String code = MemberCardStatusEnum.CLOSED.getCode();
        int i = memberCardService.updateCardStatus(cardNo, code);
        return i>0?CommonResult.success("操作成功"):CommonResult.failure("操作失败");
    }

}
